<?php
namespace Model;
require_once __DIR__."/../Lib/mydb.php";
use Lib\mydb;
use Model;
use Model\Talking;
class Talking {
    //put your code here
    public $title;
    public $description;
    public $date;   //发起时间
    public $roomid;
    private static $db=null;
    function __construct($data) {
        $this->title=$data["title"];
        $this->date=$data["date"];
        $this->description=$data["description"];
        do{
         $roomid= rand(0, 999999);
        }while(self::isonly($roomid));
        $this->roomid=$roomid;
    }
    public function add(){
        if(self::$db==null)
            self::$db =new mydb();
        $sql="insert into talking(title,`description`,roomid,date) values('".$this->title."','".$this->description."','".$this->roomid."','".$this->date."');";
        if(self::$db->exec($sql))
            return 1;
        else
            return 0;
    }
    
    public static function get($from,$to){
         if(self::$db==null)
            self::$db =new mydb();
        $sql="select * from talking order by date desc limit ".$from.",".$to."  ;";
        $res=self::$db->qurey($sql);
        $arr=array();
        while ($dd=$res->fetch_assoc()){
            $arr[]=$dd;
        }
        return $arr;
    }
    public static  function clear($time) {
       if(self::$db==null)
            self::$db =new mydb();
        $sql="delete  from talking where date <'".$time ."';";
        return self::$db->exec($sql);
    }

    public static function isonly($data) {
       if(self::$db==null)
            self::$db =new mydb();
        $sql="select * from talking where roomid=".$data.";";
        $res=self::$db->qurey($sql);
        return $res->num_rows;
    }
}